package cn.innosmart.aq.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.innosmart.aq.bean.BoxBean;

/**
 * Created by David on 2015/7/21.
 */
public class GatewayListUtil {
    private GatewayListUtil() {

    }

    public static GatewayListUtil getInstance() {
        return GatewayListUtilInstance.gatewayListUtil;
    }

    /**
     * 将字符串处理成BoxBean列表
     * [{"owner":["ben.wang"],"assignTo":["admin"],"did":"SUCU-000001-ABCEF","username":"admin","_id":"559e3208b2ae8354195e1f70","password":"123456","name":"sncu"},
     * {"owner":["admin","admin2","ben.wang"],"assignTo":[],"did":"SUCU-000000-ABCEF","username":"admin","_id":"559e31d1b2ae8354195e1f6f","password":"123456","name":"sncu"},
     * {"assignTo":[],"owner":["ben.wang"],"did":"NU5PD2NV7C7LJPXF111A","username":"admin","_id":"55adfbf0afd61de07fc2ec40","password":"admin","name":"dkcj"}]
     *
     * @param strInfo
     */
    public ArrayList<BoxBean> analyseGatewayInfo(String strInfo) {
        ArrayList<BoxBean> boxBeans = new ArrayList<BoxBean>();
        try {
//            SystemConstant.boxBeanList.clear();
            JSONArray jsonArray = new JSONArray(strInfo);
            JSONObject jsonPararm;
            BoxBean boxBean;
            ArrayList<String> assignTo;
            for (int i = 0; i < jsonArray.length(); i++) {
                boxBean = new BoxBean();
                jsonPararm = new JSONObject(jsonArray.get(i).toString());
                boxBean.setUid(jsonPararm.getString("did"));
                boxBean.setUsername(jsonPararm.getString("username"));
                boxBean.setPassword(jsonPararm.getString("password"));
                boxBean.setTitle(jsonPararm.getString("alias"));
                boxBean.setHubId(jsonPararm.getString("id"));
                //取回的BoxBean数据默认是已同步的
                boxBean.setIsSynced(true);
                String owner = jsonPararm.getString("owner");
                boxBean.setOwner(owner);
                boxBeans.add(boxBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return boxBeans;
    }

    private static class GatewayListUtilInstance {
        private static GatewayListUtil gatewayListUtil = new GatewayListUtil();
    }
}
